package com.eleadmin.common.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.system.entity.CreditRating;
import com.eleadmin.common.system.param.CreditRatingParam;
import com.eleadmin.common.system.service.CreditRatingService;
import com.eleadmin.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 信用等级表控制器
 *
 * @author EleAdmin
 * @since 2025-04-30 20:57:55
 */
@Api(tags = "信用等级表管理")
@RestController
@RequestMapping("/system/creditRating")
public class CreditRatingController extends BaseController {
    @Resource
    private CreditRatingService creditRatingService;
    @Resource
    private UserService userService;

    @OperationLog
    @ApiOperation("分页查询信用等级表")
    @GetMapping("/page")
    public ApiResult<PageResult<CreditRating>> page(CreditRatingParam param) {

        return success(creditRatingService.pageRel(param));

    }


    @OperationLog
    @ApiOperation("查询全部信用等级表")
    @GetMapping()
    public ApiResult<List<CreditRating>> list(CreditRatingParam param) {
        PageParam<CreditRating, CreditRatingParam> page = new PageParam<>(param);

        return success(creditRatingService.list(page.getOrderWrapper()));

    }


    @OperationLog
    @ApiOperation("根据id查询信用等级表")
    @GetMapping("/{id}")
    public ApiResult<CreditRating> get(@PathVariable("id") Integer id) {
        CreditRating creditRating = creditRatingService.getById(id);
        creditRating.setUserName(userService.getById(creditRating.getUserId()).getNickname());
        return success(creditRating);

    }

    @OperationLog
    @ApiOperation("根据用户id查询信用等级表")
    @GetMapping("/getByUserId/{userId}")
    public ApiResult<CreditRating> getByUserId(@PathVariable("userId") Integer userId) {
        QueryWrapper<CreditRating> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        CreditRating creditRating = creditRatingService.getOne(queryWrapper);

        if (creditRating != null) {
            creditRating.setUserName(userService.getById(creditRating.getUserId()).getNickname());
            return success(creditRating);
        } else {
            return fail("未找到用户信用等级信息", (CreditRating) null);
        }

    }

    @OperationLog
    @ApiOperation("添加信用等级表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CreditRating creditRating) {
        if (creditRatingService.save(creditRating)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @OperationLog
    @ApiOperation("修改信用等级表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CreditRating creditRating) {
        if (creditRatingService.updateById(creditRating)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @OperationLog
    @ApiOperation("删除信用等级表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (creditRatingService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


    @OperationLog
    @ApiOperation("批量添加信用等级表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<CreditRating> list) {
        if (creditRatingService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @OperationLog
    @ApiOperation("批量修改信用等级表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<CreditRating> batchParam) {
        if (batchParam.update(creditRatingService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @OperationLog
    @ApiOperation("批量删除信用等级表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (creditRatingService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
